package com.zbkj.front.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbkj.dal.base.enums.BizTypeEnum;
import com.zbkj.dal.base.enums.BooleanEnums;
import com.zbkj.dal.dao.BizImgMapper;
import com.zbkj.dal.dao.CooperateMerchantDao;
import com.zbkj.dal.dao.CooperateStoreDao;
import com.zbkj.dal.entity.CooperateMerchant;
import com.zbkj.dal.entity.CooperateStore;
import com.zbkj.dal.entity.ba.BizImg;
import com.zbkj.front.model.vo.CooperateMerchantInfoVO;
import com.zbkj.front.model.vo.CooperateMerchantListVO;
import com.zbkj.front.service.CooperateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CooperateServiceImpl implements CooperateService {

    @Resource
    private CooperateMerchantDao cooperateMerchantDao;

    @Resource
    private CooperateStoreDao cooperateStoreDao;

    @Resource
    private BizImgMapper bizImgMapper;

    @Override
    public List<CooperateMerchantListVO> cooperateMerchant() {
        List<CooperateMerchantListVO> cooperateMerchantList = new ArrayList<>();
        List<CooperateMerchant> cooperateMerchants = cooperateMerchantDao.selectList(new LambdaQueryWrapper<>());
        List<CooperateStore> cooperateStores = cooperateStoreDao.selectList(new LambdaQueryWrapper<>());
        Map<Integer, List<CooperateStore>> cmGroup = cooperateStores.stream().collect(Collectors.groupingBy(CooperateStore::getCmId));
        Map<String, List<CooperateMerchant>> industryGroup = cooperateMerchants.stream().collect(Collectors.groupingBy(CooperateMerchant::getIndustry));
        industryGroup.forEach((industry, list) -> {
            CooperateMerchantListVO cooperateMerchantListVO = new CooperateMerchantListVO();
            cooperateMerchantListVO.setIsOwn(list.get(0).getOwn());
            cooperateMerchantListVO.setIndustry(industry);
            List<CooperateMerchantListVO.CooperateMerchant> merchants = list.stream().map(cooperateMerchant -> {
                List<CooperateStore> stores = cmGroup.get(cooperateMerchant.getId());
                CooperateMerchantListVO.CooperateMerchant merchant = new CooperateMerchantListVO.CooperateMerchant();
                merchant.setCooperateMerchantCode(cooperateMerchant.getCooperateMerchantCode());
                merchant.setBrand(cooperateMerchant.getMerchantName());
                merchant.setStoreNum(CollectionUtil.isEmpty(stores) ? 0 : stores.size());
                merchant.setLogoUrl(cooperateMerchant.getLogoUrl());
                return merchant;
            }).collect(Collectors.toList());
            cooperateMerchantListVO.setCooperateMerchants(merchants);
            cooperateMerchantList.add(cooperateMerchantListVO);
        });
        return cooperateMerchantList;
    }

    @Override
    public CooperateMerchantInfoVO cooperateMerchantInfo(String cooperateMerchantCode) {
        LambdaQueryWrapper<CooperateMerchant> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CooperateMerchant::getCooperateMerchantCode, cooperateMerchantCode);
        wrapper.eq(CooperateMerchant::getDeleted, BooleanEnums.FALSE.getCode());
        CooperateMerchant cooperateMerchant = cooperateMerchantDao.selectOne(wrapper);

        LambdaQueryWrapper<CooperateStore> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CooperateStore::getCmId, cooperateMerchant.getId());
        List<CooperateStore> cooperateStores = cooperateStoreDao.selectList(queryWrapper);
        List<CooperateMerchantInfoVO.Store> stores = cooperateStores.stream().map(store -> {
            CooperateMerchantInfoVO.Store cooperateStore = new CooperateMerchantInfoVO.Store();
            cooperateStore.setStoreName(store.getStoreName());
            cooperateStore.setAddress(store.getAddress());
            cooperateStore.setOpeningTime(store.getOpeningTime());
            cooperateStore.setClosingTime(store.getClosingTime());
            cooperateStore.setMobile(store.getMobile());
            cooperateStore.setLatitude(store.getLatitude());
            cooperateStore.setLongitude(store.getLongitude());
            return cooperateStore;
        }).collect(Collectors.toList());

        // 获取图片
        LambdaQueryWrapper<BizImg> imgWrapper = new LambdaQueryWrapper<>();
        imgWrapper.eq(BizImg::getBizId, cooperateMerchant.getId());
        imgWrapper.eq(BizImg::getType, BizTypeEnum.COOPERATE_MERCHANT.getType());
        List<BizImg> images = bizImgMapper.selectList(imgWrapper);
        List<String> imagePath = images.stream().map(BizImg::getPath).collect(Collectors.toList());

        CooperateMerchantInfoVO cooperateMerchantInfoVO = new CooperateMerchantInfoVO();
        cooperateMerchantInfoVO.setCooperateMerchantName(cooperateMerchant.getMerchantName());
        cooperateMerchantInfoVO.setSynopsis(cooperateMerchant.getSynopsis());
        cooperateMerchantInfoVO.setOfficialWebsite(cooperateMerchant.getOfficialWebsite());
        cooperateMerchantInfoVO.setStoreList(stores);
        cooperateMerchantInfoVO.setStorePhoto(imagePath);
        return cooperateMerchantInfoVO;
    }

}
